//
//  PreviewViewController.swift
//  zhendeBusiness
//
//  Created by 陆文宇 on 2017/6/1.
//  Copyright © 2017年 chenliang. All rights reserved.
//

import UIKit

class PreviewViewController: YYController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    var dataSource = [RichModel]()
    
    override func yy_configNaviItem(_ naviItem: UINavigationItem) {
        super.yy_configNaviItem(naviItem)
        
        title = "预览"
        
        let closeItem = UIBarButtonItem(title: "关闭", style: .done, target: self, action: #selector(close))
        navigationItem.leftBarButtonItem = closeItem
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.register(UINib(nibName: "RichPreviewTableViewCell", bundle: nil), forCellReuseIdentifier: richCellIdentifier)
    }
    
    @objc func close() {
        yy_dismiss()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: -TableViewDelegate
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: richCellIdentifier, for: indexPath) as! RichPreviewTableViewCell
        let content = dataSource[indexPath.row]
        if content.richType == .txt {
            cell.richType = .txt
            cell.richLabel?.text = content.txt
        } else {
            cell.richType = .image
            if let image = content.image {
                cell.richImageView.image = image
            } else {
                cell.richImageView.kf.setImage(with: URL(string: content.imageUrl!)!, placeholder: UIImage(named: "placeImage2-1"), completionHandler: { (image, error, _, _) in
                    if error == nil {
                        if content.image == nil {
                            content.image = image
                            if tableView.indexPathsForVisibleRows?.contains(indexPath) ?? false {
//                                tableView.reloadRows(at: [indexPath], with: .none)
                                tableView.reloadData()
                            }
                        }
                    }
                })
            }
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let content = dataSource[indexPath.row]
        if content.richType == .txt {
            return (content.txt! as NSString).boundingRect(with: CGSize(width: screenWidth - 20, height: CGFloat.infinity), options: [.usesLineFragmentOrigin], attributes: [NSAttributedStringKey.font: mainFont], context: nil).size.height + 10
        } else {
            if let image = content.image {
                return (screenWidth - 20) / image.size.width * image.size.height + 10
            } else {
                return (screenWidth - 20) / 3 * 2 + 10
            }
        }
    }
    
    func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
        return false
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
